package com.googlecode.jmicroerp.repository;

/**
 *
 * @author alex
 */
public class GenericProperty {

    private String propertyName;
    private GenericProperty reference;

    public static GenericProperty getInstance(String expression) {
        String property = expression;
        String[] props = property.split("\\.");
        GenericProperty genericProperty = new GenericProperty(props[0]);

        if (props.length > 0) {
            GenericProperty last = genericProperty;
            for (int i = 1; i < props.length; i++) {
                GenericProperty newProperty = new GenericProperty(props[i]);
                last.setReference(newProperty);
                last = newProperty;
            }
        }
        return genericProperty;
    }

    public GenericProperty(String propertyName) {
        this.propertyName = propertyName;
    }

    public String getPropertyName() {
        return propertyName;
    }

    public void setPropertyName(String propertyName) {
        this.propertyName = propertyName;
    }

    public GenericProperty getReference() {
        return reference;
    }

    public GenericProperty setReference(GenericProperty reference) {
        this.reference = reference;
        return this;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((propertyName == null) ? 0 : propertyName.hashCode());
        result = prime * result
                + ((reference == null) ? 0 : reference.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        GenericProperty other = (GenericProperty) obj;
        if (propertyName == null) {
            if (other.propertyName != null) {
                return false;
            }
        } else if (!propertyName.equals(other.propertyName)) {
            return false;
        }
        if (reference == null) {
            if (other.reference != null) {
                return false;
            }
        } else if (!reference.equals(other.reference)) {
            return false;
        }
        return true;
    }
}
